package qf.wlh.com.oneprisdent.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

import bean.GridViewSpeakerBean;
import fragment.detialfragment.SpeakerDetialFragment;
import qf.wlh.com.oneprisdent.R;

/**
 * Created by zyq on 2016/11/25.
 */

//我是
public class SpeakerAdapter extends PagerAdapter {
    private List<GridViewSpeakerBean.DataBean> data=new ArrayList<>();
    private Context context;
    private GridView gridView;
    private FragmentManager fragmentManager;

    public SpeakerAdapter(List<GridViewSpeakerBean.DataBean> data, Context context,FragmentManager fragmentManager) {
        this.data = data;
        this.context = context;
        this.fragmentManager=fragmentManager;
    }

    @Override
    public int getCount() {
        return data!=null?data.size():0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {

        View view=LayoutInflater.from(context).inflate(R.layout.grid_spreaker_layout,null);
        gridView= (GridView) view.findViewById(R.id.gridview);
        SpeakerGridAdapter adapter=new SpeakerGridAdapter(data,context);
        gridView.setAdapter(adapter);
        container.addView(view);
        initListener();
        return view;
    }

    private void initListener() {
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle=new Bundle();
                int id=data.get(i).getLectures_with_cover().get(0).getId();
                bundle.putInt("key",id);

                SpeakerDetialFragment speakerDetialFragment=new SpeakerDetialFragment();
                speakerDetialFragment.setArguments(bundle);
                fragmentManager.beginTransaction()
                        .replace(R.id.container,speakerDetialFragment,"SpeakerDetialFragment")
                        .commit();
            }
        });
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof View ){
            View view = (View) object;
            container.removeView(view);
        }
    }

}
